/*****************************************************************************
 * $Workfile:: led.c                                                       $
 * $Revision:: 19                                                            $
 * $JustDate::  3/21/12                                                      $
 *
 *      Hybrid
 *      Overload LED interface
 *****************************************************************************/


#include "common.h"
#include "main.h"
#include "led.h"
#include "control.h"
#include "fet.h"
#include "nvm.h"
#include "serial.h"
#include "switches.h"
#include "alarm.h"

//
//  LED rate, in beeps per minute.
//  The rate is calculated with main cycle 10 millisecond
//                                              rate
//                                           <----------->
//                                            rate/2
//                                           <----->
//                                           -------     ------
//                                           |     |     |
//                                      // ---     -------
//  LED_RATE = ( milliesond_per_minute / main_cycle_in_miliseconds ) / (beeps_per_minute * 2)
//
#define LED_RATE(x)       ( (60000l/SYSTEM_CYCLE) / ((x) * 2))
#define LED_RATE_60           LED_RATE(60)      // (60000 / 10) / 60 * 2
#define LED_RATE_120          LED_RATE(120)
#define  LED_RATE_180         LED_RATE(180) //15 // 17 changed to be multiple of 50 milliseconds 
#define  LED_RATE_240         LED_RATE(240) //10 // 12 changed to be multiple of 50 milliseconds   

#define OVERLOAD_CUTOUT_OPTION lift_options.fOverload2_cutout    // disabled

struct led LED_Data;

#define GROUND_LED   LED_Data.fGround

static const struct
{
   UINT8       bCount;
   UINT8    bRate;
   UINT16      wOff;
}
LED_TimeTable[] =
{
   {1, LED_RATE_240, LED_RATE_240}       // 0 LED_OVERLOAD 100%
   , {1, LED_RATE_120, LED_RATE_120}     // 1 LED_OVERLOAD 90%
   , {1, LED_RATE_60, LED_RATE_60}     // 2 LED_OVERLOAD 80%
};

/*******************************************************************************
 *
 * PROCEDURE:  LED_Process()
 *
 * DESCRIPTION: It generates led output on ground panel
 *              All events are preocessed by priority level.
 *
 * VARIABLE NAME  R/W DESCRIPTION
 * ---------------- --- --------------------------------------------------------
 * ALARM_Data       R/W owner
 ******************************************************************************/

void
LED_Process(void)
{
   enum LED_TYPE eCode;

   if(SLEEP_Data.fSleep)
   {
      eCode = LED_TURN_OFF;
   }
   else if(LIFT_Data.fLoad99)
   {
      eCode = LED_OVERLOAD_100;
   }
   else if(LIFT_Data.fLoad90)
   {
      eCode = LED_OVERLOAD_90;
   }
   else if(LIFT_Data.fLoad80)
   {
      eCode = LED_OVERLOAD_80;
   }
   else
   {
      eCode = LED_TURN_OFF;
   }


   //
   //---------   Process the ALARM output
   //
   if(LED_Data.wDelay)
   {
      LED_Data.wDelay--;
   }
   else
   {
      switch(LED_Data.eState)
      {
         default:
         case LED_GET_STATE:
            LED_Data.eCode = eCode;

            if(eCode >= LED_TURN_OFF)
            {
               break;
            }

            LED_Data.bCount = LED_TimeTable[ LED_Data.eCode].bCount;

         case  LED_FLASH_ON:
            GROUND_LED = 1;
            //              PLATFORM_ALARM  = ALARM_TimeTable[ ALARM_Data.eCode].fPlatform;
            LED_Data.wDelay = LED_TimeTable[ LED_Data.eCode].bRate;
            LED_Data.eState = LED_FLASH_OFF;
            break;

         case  LED_FLASH_OFF:
            GROUND_LED = 0;

            //              PLATFORM_ALARM = 0;
            if(LED_Data.bCount > 1)
            {
               LED_Data.bCount--;
               LED_Data.wDelay = LED_TimeTable[ LED_Data.eCode].bRate;
               LED_Data.eState = LED_FLASH_ON;
            }
            else
            {
               LED_Data.wDelay = LED_TimeTable[ LED_Data.eCode].wOff;
               LED_Data.eState = LED_GET_STATE;
            }

            break;
      }
   }
}

